package com.ruoyi.cmdb.controller;

import com.ruoyi.cmdb.service.MessageService;
import com.ruoyi.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteWatchdog;
import org.apache.commons.exec.PumpStreamHandler;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;
import java.io.StringReader;
import java.util.Objects;

/**
 * 测试rabbitmq
 *
 */
@RestController
@RequestMapping("/cmdb/testRabbitmq")
@Api(tags = "2.testRabbitmq")
public class TestRabbitmqController {
    @Autowired
    private MessageService messageService;

    /**
     * testRabbitmq send
     */
    @PostMapping("/send/{id}")
    @ApiOperation("send")
    public AjaxResult send(@PathVariable String id)  {
        String exchange="cmdbExchange";
        String connection="cmdbtestConnection";
        messageService.sendMessage(exchange,connection,id);

        String rps="rabbitmq send success sout";
        System.out.println(rps);
        return AjaxResult.success(rps);
    }

    /**
     * testRabbitmq sendManual
     */
    @PostMapping("/sendManual/{id}")
    @ApiOperation("sendManual")
    public AjaxResult sendManual(@PathVariable String id)  {
        String exchange="cmdbExchangeManual";
        String connection="cmdbtestConnectionManual";
        messageService.sendMessage(exchange,connection,id);

        String rps="rabbitmq "+connection+"send success sout";
        System.out.println(rps);
        return AjaxResult.success(rps);
    }

    /**
     * testRabbitmq getManual
     */
    @PostMapping("/getManual/{queue}")
    @ApiOperation("getManual")
    public AjaxResult getManual(@PathVariable String queue)  {
        String id = messageService.doMessage(queue);
        String rps="";
        if (Objects.equals(id, "null")){
            rps="rabbitmq :"+queue+" receive success sout,id:"+id;
        }else{
            rps="rabbitmq :"+queue+" receive success sout,id:"+id;
        }
        System.out.println(rps);
        return AjaxResult.success(rps);
    }
}
